import { downloadFileModel, shareDataModel } from '@/views/products/brandBreakout/breakout.module';

export function share(options: shareDataModel) {
    return new Promise((resolve, reject) => {
        window.xcBridge?.share({
            params: options,
            success: (res: object) => {
                resolve(res);
            },
            fail: (err: object) => {
                reject(err);
            },
        });
    });
}

/**
 * 下载文件
 * @param {object} options: {url: string, params: object}
 * web： url: '任何网页地址'
 */
export const downloadFile = (options: downloadFileModel) =>
    new Promise((resolve, reject) => {
        window.xcBridge?.downloadFile({
            params: {
                url: options.url,
                params: options.params,
            },
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });

// 跳转 原生页面的路径
const Prefix = 'xinchao://';
export const NativePath = {
    LOGIN: `${Prefix}/common/loginActivity`, // 登陆
    /**
     * 客户详情 页面需要穿的参数
     * customerId: item.customerId, // iOS
        type: this.$route.query.type || '', // iOS
        // eslint-disable-next-line camelcase
        key_custom: item.customerId, // Android
        // eslint-disable-next-line camelcase
        key_custom_one_page: true, // Android
        fromType: 1, // 我的应收盘点: 1, 我部门的应收盘点: 2,top300客户: 3, M线-我的客户: 4, 我的客户列表 和 我部门的客户列表 等其它地方进入客户详情界面: 0
     */
    CUSTOMER_DETAIL: `${Prefix}/custom/CustomDetailsActivity`, // 客户详情
    BACK: `${Prefix}back`, // 返回上一页固定搭配
    CLOSE: `${Prefix}close`, // 关闭当前页面
    CHOOSE_DELIVERY_SUBJECT: `${Prefix}/custom/CustomNameChooseActivity`, // 跳转选择投放主体页面
};

export const openUrl = (options: { params: { [key: string]: unknown }; url: string; }) =>
    new Promise((resolve, reject) => {
        try {
            let params = { ...options.params };
            if (options.url === NativePath.BACK) {
                params = { saved: false, ...options.params };
            }
            window.xcBridge.openUrl({
                params: {
                    url: options.url,
                    params: params,
                },
                success: (res: unknown) => {
                    resolve(res);
                },
                fail: (err: unknown) => {
                    reject(err);
                },
            });
        } catch (err) {
            reject(err);
        }
    });

export const getGeoLocation = (data = { sync: true }) => {
    if (window.xcBridge) {
        return new Promise((resolve, reject) => {
            if (window.xcBridge) {
                window.xcBridge.getGeoLocation({
                    params: data,
                    success: res => {
                        resolve(res);
                    },
                    fail: err => {
                        reject(err);
                    },
                });
            } else {
                reject(new Error('当前环境不支持获取定位信息。'));
            }
        });
    }
    throw new Error('未找到jssdk');
};

export interface posData {
    lng: number;
    lat: number;
    address: string;
    city: string;
}

export const getGeoLocationOnReady = () => {
    return new Promise((r, j) => {
        try {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
            window.xcBridge.ready(() => {
                getGeoLocation().then((data) => {
                    r(data);
                }).catch(e => {
                    console.log('获取失败', e);
                    j('error');
                });
            });
        } catch {
            const timer = setTimeout(() => {
                j('error');
            }, 2000);
            document.addEventListener('jsBridgeReady', () => {
                clearTimeout(timer);
                getGeoLocation().then((data) => {
                    r(data);
                }).catch(e => {
                    console.log('获取失败', e);
                    j('error');
                });
            });
        }

    });
};
